package moonhy.c2dm_client;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import moonhy.c2dm_client.dw.MemberData;
import moonhy.c2dm_client.util.httpConnectionManager;

import org.json.JSONArray;
import org.json.JSONObject;
import org.taptwo.android.widget.TitleProvider;

import android.content.Context;
import android.os.AsyncTask;
import android.text.util.Linkify;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;


public class MemberListAsyncAdapter extends BaseAdapter implements TitleProvider {

	private LayoutInflater mInflater;
	private Context context;
	private static final DateFormat dfTitle = new SimpleDateFormat("E, dd MMM");
	
	private static final int daysDepth = 5;
	private static final int daysSize = daysDepth * 2 + 1;
	
	//private static Date[] dates = new Date[ daysSize ];
	private static String[] content = new String[ daysSize ];
	String[] memberName = new String[ daysSize ];
	private ArrayList<MemberData> md_array;
	
	
	private class ViewHolder {
		ProgressBar mProgressBar;
		View mContent;
		TextView mPhone;
		TextView mBirth;
		TextView mDesc;
	}
	
	
	public MemberListAsyncAdapter(Context context, Object result) {
		this.context = context;
		md_array = (ArrayList<MemberData>) result;
		
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		//prepareDates();
		//new initPage().execute();
		//setData();
	}
	
	@Override
	public String getItem(int position) {
		return md_array.get(position).getMemberName();
	}

	@Override
	public long getItemId(int position) {
		return position; 
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return drawView(position, convertView);
	}

	private View drawView(int position, View view) {
		ViewHolder holder = null;
		
		if(view == null) {
			view = mInflater.inflate(R.layout.member_list_sub, null);
			
			holder = new ViewHolder();

			holder.mProgressBar = (ProgressBar) view.findViewById(R.id.progress);
			holder.mPhone = (TextView) view.findViewById(R.id.member_phone);
			holder.mBirth = (TextView) view.findViewById(R.id.member_birth);
			holder.mDesc = (TextView) view.findViewById(R.id.member_desc);
			holder.mContent = (View) view.findViewById(R.id.content);
			
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		

		//final String o = getItem(position);
		MemberData o = md_array.get(position);
		if (o != null) {
			holder.mProgressBar.setVisibility(View.GONE);
			//holder.mName.setText(o);
			//holder.mName.setText(o.getMemberName());
			holder.mPhone.setText(o.getMemberPhone());
			Linkify.addLinks(holder.mPhone, Linkify.PHONE_NUMBERS);
			holder.mBirth.setText(o.getMemberBirth());
			holder.mDesc.setText(o.getMemberDesc());
			holder.mContent.setVisibility(View.VISIBLE);
		}
		else {
			new LoadContentTask().execute(position, view);

			holder.mContent.setVisibility(View.GONE);
			holder.mProgressBar.setVisibility(View.VISIBLE);
		}
	
		return view;
	}

	@Override
	public String getTitle(int position) {
		return md_array.get(position).getMemberName();
	}

	@Override
	public int getCount() {
		return md_array.size();
	}

	private class LoadContentTask extends AsyncTask<Object, Object, Object> {
		
		private Integer position;
		private View view;
		
		@Override
		protected Object doInBackground(Object... arg) {
			position = (Integer) arg[0];
			view = (View) arg[1];

// long-term task is here 			
			try {
				Thread.sleep(3000); // do nothing for 3000 miliseconds (3 second)
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			return getTitle(position);
		}

		protected void onPostExecute(Object result) {
			// process result    	 
			//content[position] = (String) result;
			
	    	drawView(position, view);

	    	view.postInvalidate();
	     }

	}	

}
